import fsTools from '@/utils/FileRWAdapter.js';
import { gzipSync, gunzipSync } from 'fflate';

export class StorageManager {

    static bitEncoder=new TextEncoder();
    static bitDecoder=new TextDecoder();

    static save(savefileId, jsonString) {
        let data = gzipSync(this.bitEncoder.encode(jsonString));
        let src=fsTools.filePath(savefileId);
        fsTools.writeFile(src, data);
    }

    static load(savefileId) {
        let src=fsTools.filePath(savefileId);
        if(fsTools.exists(src)){
            let data=fsTools.readFile(src);
            return this.bitDecoder.decode(gunzipSync(data));
        }
        return null;
    }

    static exists(savefileId) {
        return fsTools.exists(fsTools.filePath(savefileId));
    }

    static remove(savefileId) {
        let src=fsTools.filePath(savefileId);
        if(fsTools.exists(src)){
            fsTools.remove(src);
        }
    }
}